package eclipse;

import java.util.Arrays;
import java.util.Random;

public class ArrayExercise05 {
    public static void main(String[] args) {
        //从方法，获得红球号码数组和蓝球号码数组
        int[] redBall = preparedRedArray(33);
        int[] blueBall = preparedRedArray(16);
        System.out.println(Arrays.toString(redBall));
        System.out.println(Arrays.toString(blueBall));
        //从红球号码数组选择6个，保存到变量red
        int[] red6Ball = choose6RedBall(redBall);
        System.out.println(Arrays.toString(red6Ball));
        int blue1Ball = choose1BlueBall(blueBall);
        System.out.println(blue1Ball);
    }

    private static int choose1BlueBall(int[] blueBall) {
        return blueBall[new Random().nextInt(16)];
    }

    private static int[] choose6RedBall(int[] redBall) {
        int[] red6 = new int[6];
        boolean[] flag = new boolean[33];
        for(int i = 0; i < red6.length; i++){
            //先随机选择一个位置
            //在判断是否重复再选另一个位置
            int j;
            do{
                j = new Random().nextInt(33);
            }while(flag[j]);
            red6[i] = redBall[j];
            flag[j] = true;// 标记j位置已经选过
        }
        return red6;
    }

    private static int[] preparedRedArray(int len) {
        /*
        1. 新建len的长度的int[]数组，保存到变量arr
        2. 遍历数组，填入1到len值
        3. 返回数组arr
         */
        int[] arr = new int[len];
        for(int i = 0; i < arr.length; i++){
            arr[i] = i + 1;
        }
        return arr;
    }
}
